package net.butfly.bus.server.router;

import java.util.HashMap;
import java.util.Map;

import net.butfly.bus.config.bean.RouterBean;
import net.butfly.bus.server.ServerConfig;
import net.butfly.bus.server.router.impl.SimpleDirectory;


/**
 * 目录构建器
 * 
 */
public class DirectoryBuilder {
	private static DirectoryBuilder ins = new DirectoryBuilder();

	public static DirectoryBuilder getInstance() {
		return ins;
	}

	/**
	 * 缓存的目录方案
	 */
	private Map<String, DirectoryPolicy> insMap = new HashMap<String, DirectoryPolicy>();

	public DirectoryPolicy getPolicy() {
		RouterBean routerBean = ServerConfig.getRouterBean();
		DirectoryPolicy directory = insMap.get(routerBean.getDirectory());
		if (directory == null) {
			synchronized (this) {
				if (RouterType.isDirectoryOfSimple(routerBean.getDirectory())) {
					directory = new SimpleDirectory();
				}
				if (directory != null) {
					insMap.put(routerBean.getDirectory(), directory);
				}
			}
		}
		return directory;
	}
}
